<?php
function showCars($cars)
{
    $clength = count($cars);
    for ($x = 0; $x < $clength; $x++) {
        echo $cars[$x];
        echo "<br>";
    }
}
function showNumbers($numbers){
    $clength=count($numbers);
    for($x=0;$x<$clength;$x++)
    {
        echo $numbers[$x];
        echo "<br>";
    }
}
function showAges($age)
{
    foreach ($age as $x => $x_value) {
        echo "Key=" . $x . ", Value=" . $x_value;
        echo "<br>";
    }
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP Sorting Arrays</title>
</head>
<body>
<h1>PHP Sorting Arrays</h1>
<div>
    <h3>Sort Array in Ascending Order - sort()</h3>
    <?php
    $cars = array("Volvo","BMW","Toyota");
    sort($cars);
    showCars($cars);
    $numbers = array(4,6,2,22,11);
    sort($numbers);
    showNumbers($numbers);
    ?>
</div>
<div>
    <h3>Sort Array in Descending Order - rsort()</h3>
    <?php
    rsort($cars);
    showCars($cars);
    rsort($numbers);
    showNumbers($numbers);
    ?>
</div>
<div>
    <h3>Sort Array in Ascending Order, According to Value - asort()</h3>
    <?php
    $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
    asort($age);
    showAges($age);
    ?>
</div>
<div>
    <h3>Sort Array in Ascending Order, According to Key - ksort()</h3>
    <?php
    ksort($age);
    showAges($age);
    ?>
</div>
<div>
    <h3>Sort Array in Descending Order, According to Value - arsort()</h3>
    <?php
    arsort($age);
    showAges($age);
    ?>
</div>
<div>
    <h3>Sort Array in Descending Order, According to Key - krsort()</h3>
    <?php
    krsort($age);
    showAges($age);
    ?>
</div>
</body>
</html>